package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.utils.UserVoUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    MenuService menuService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findPage(queryPageBean);
    }




    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> all = menuService.findAll();
            /*List<List<Menu>> list=new ArrayList<>();
            list.add(all);*/
            List<Menu> dataMenu = UserVoUtils.getMenu(all);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,dataMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
           Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
           menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(Integer roleId) {
        try {
            List<Integer> all = menuService.findMenuByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findLevel")
    public Result findLevel() {
        try {
            List<Menu> all = menuService.findLevel();
            /*List<List<Menu>> list=new ArrayList<>();
            list.add(all);*/
            List<Menu> dataMenu = UserVoUtils.getMenu(all);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,dataMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
           menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            String delete = menuService.delete(id);
            return new Result(true,delete);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

}
